package cn.bocweb.net;

import com.google.gson.Gson;

import java.util.logging.Logger;

import cn.bocweb.encryption.AES256Utils;

/**
 * Created by kaizen on 15/11/27.
 */
public class BocResponse {

    private static BocResponse instance;
    private static String jsonString;
    public static BocResponse getInstance() {
        if (instance == null) {
            synchronized (BocResponse.class) {
                if (instance == null) {
                    instance = new BocResponse();
                }
            }
        }
        return instance;
    }

    public <T> T getContent(String content, Class<T> cls) {
        String json = null;
        try {
            json = AES256Utils.getInstance().decryptSecure(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonString = json;//查看解码后的json数据
        show(json);
        return new Gson().fromJson(String.format("{\"data\":%s}", json), cls);
    }

    private void show(String str) {
        str = str.trim();
        int index = 0;
        int maxLength = 40000;
        String sub;
        while (index < str.length()) {
            if (str.length() <= index + maxLength) {
                sub = str.substring(index);
            } else {
                sub = str.substring(index, maxLength);
            }
            index += maxLength;
            Logger.getLogger("json").info(String.format("{\"data\":%s}", sub.trim()));
        }
    }
    /**
     * 为了查看解码后的json数据
     */

  public static String showJson(){
      return  jsonString;
  }

}
